﻿Imports System.Data.OleDb

Public Class CalculateBMI
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub



    Protected Sub btnCalculateBMI_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculateBMI.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim result = getBMI(txtWeight.Text, txtHeight.Text)
        lblBMI.Text = result

        Select Case result
            Case Is <= 16.5
                lblResult.Text = "Your BMI is " & result & ", You are Severely Underweight! <br/> Consult a Health Specialist and Increase your weight to achieve a Normal BMI! <br/> Refer to the Chart Below for your Ideal BMI!"
            Case Is <= 18.5
                lblResult.Text = "Your BMI is " & result & ", You are Underweight! <br/> Consult a Health Specialist and Increase your weight to achieve a Normal BMI! <br/> Refer to the Chart Below for your Ideal BMI!"
            Case Is <= 25
                lblResult.Text = "Your BMI is " & result & ", You have a Normal BMI. <B>CONGRATULATIONS!</B> <br/> Exercise Regularly and Eat a Healthy Diet to Maintain your BMI! <br/> Refer to the Chart Below for your Ideal BMI!"
            Case Is <= 30
                lblResult.Text = "Your BMI is " & result & ", You are Overweight! <br/> Consult a Health Specialist and Decrease your weight to achieve a Normal BMI! <br/> Refer to the Chart Below for your Ideal BMI!"
            Case Is <= 35
                lblResult.Text = "Your BMI is " & result & ", You are Obese! <br/> Consult a Health Specialist and Decrease your weight to achieve a Normal BMI! <br/> Refer to the Chart Below for your Ideal BMI!"
            Case Is <= 40
                lblResult.Text = "Your BMI is " & result & ", You are Clinically Obese! <br/> Consult a Health Specialist and Decrease your weight to achieve a Normal BMI! <br/> Refer to the Chart Below for your Ideal BMI!"
            Case Is > 40
                lblResult.Text = "Your BMI is " & result & ", You are Dangerously Obese! <br/> Consult a Health Specialist and Decrease your weight to achieve a Normal BMI! <br/> Refer to the Chart Below for your Ideal BMI!"

        End Select



        Dim bmiSQL As String = "insert into tblBMI (Height, Weight, BMI, bmiDate, UserName) values (@f1, @f2, @f3, @f4, @f5)"
        Dim cmdBMI As OleDbCommand = New OleDbCommand(bmiSQL, oleDbConn)
        cmdBMI.CommandType = CommandType.Text

        cmdBMI.Parameters.AddWithValue("@f1", txtHeight.Text)
        cmdBMI.Parameters.AddWithValue("@f2", txtWeight.Text)
        cmdBMI.Parameters.AddWithValue("@f3", lblBMI.Text)
        cmdBMI.Parameters.AddWithValue("@f4", Date.Today)

        If User.Identity.IsAuthenticated Then
            cmdBMI.Parameters.AddWithValue("@f5", User.Identity.Name)
        Else
            cmdBMI.Parameters.AddWithValue("@f5", "Anonymous")
        End If
        cmdBMI.ExecuteNonQuery()

        Image2.ImageUrl = "~/images/bmi_table.jpg"
    End Sub


    Function getBMI(ByVal strWeight As String, ByVal strHeight As String) As String
        Dim Result, Weight, Height As Double
        Weight = CDbl(strWeight)
        Height = CDbl(strHeight)
        Result = CDbl(Weight / (Height ^ 2))
        Return Result
    End Function
End Class